import router from "@/router";
import { Message } from "element-ui";
import Cookie from "js-cookie";
import Apiadmin from "../../api/admin"
export default {
    state() {   //存储变量
        return {
            user: {}, //用户信息
            userInfo: {}
        }
    },
    // //自定义指令-权限按钮
    getters: {
        permissionList: (state) => {
            return state.user.perms
        }
    },
    mutations: {  //提交更新数据的方法
        setUser(state, user) {
            state.user = user;
        },
        setUserInfo(state, ruleForm) {
            state.userInfo = ruleForm
        },
        //管理员退出登录
        loginOut(state) {
            console.log(Apiadmin)
            Apiadmin.loginOut().then(res => {
                console.log(res)
                let { errno, errmsg } = res;
                let path = router.currentRoute.fullPath  //获取当前路由
                if (errno == 200) {
                    Message.success("退出成功");
                    state.user = {}; //清空状态存储
                    Cookie.remove("AdminToken"); //删除token 
                    router.replace({
                        path: '/login',   //跳转登录页面
                        query: { skip: path }   //标记   从其它页面退出登录，登录成功返回其它页面
                    })
                } else {
                    Message.error(errmsg)
                }
            }).catch(err => {
                console.log(err)
            })
        },
    },
    actions: {
        setUser(context, user) {
            context.commit('setUser', user)
        },
        setUserInfo(context, ruleForm) {
            context.commit('setUserInfo', ruleForm)
        },
        loginOut(context, user) {

            context.commit('loginOut', user)
        },

    }
}